package com.lixinlei.security.order.controller;

import com.lixinlei.security.order.vo.OrderInfo;
import com.lixinlei.security.order.vo.PriceInfo;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/orders")
@Slf4j
public class OrderController {

    private RestTemplate restTemplate = new RestTemplate();

    /**
     * 通过注解 @RequestHeader，就可以让 order 服务，验完 token 后，知道是哪个用户发的这次请求；
     * @param info
     * @param username
     * @return
     */
    @PostMapping
    public OrderInfo create(@RequestBody OrderInfo info, @AuthenticationPrincipal String username) {
        log.info("user is " + username);
    //		PriceInfo price = restTemplate.getForObject("http://localhost:9060/prices/"
    //                + info.getProductId(), PriceInfo.class);
    //		log.info("price is " + price.getPrice());
        return info;
    }

    @GetMapping("/{id}")
    public OrderInfo getInfo(@PathVariable Long id, @RequestHeader String username) {
        log.info("user is " + username);
        log.info("orderId is " + id);
        OrderInfo info = new OrderInfo();
        info.setId(id);
        info.setProductId(id * 5);
        return info;
    }

}
